package com.greenhouse9.prog04;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;


public class Main {
    public static void main(String[] args){

    System.out.println( "�ϐ��̐���" + args.length + "�ł��B");

    for (int i=0; i<args.length; i++){
        System.out.println("�ϐ� arg[" + i + "] �� " + args[i] + " �ł��B");
    }

    /* ��̐����`�F�b�N */
    if (args.length < 1) {
        System.out.println("Usage: java Main filename1 ");
        System.exit(0);
    }

    File f1    = new File(args[0]);

    /* Check file exists */
    if (!f1.exists()) {
        System.out.println(f1.getName() + "�͑��݂��܂���B");
        System.exit(0);
    }

    //�t�@�C�����擾
    System.out.println("�t�@�C����: "   + f1.getName() + " �ł��B");
    System.out.println("�p�X��:�@"      + f1.getPath() + "�@�ł��B");
    System.out.println("��΃p�X���F�@" + f1.getAbsolutePath() );
    System.out.println("�e�p�X���F�@"   + f1.getParent());
    try {
        System.out.println("���K�p�X���F�@" + f1.getCanonicalPath() );
    }
    catch (IOException e){
        e.printStackTrace();
    }

    //�t�@�C�������擾
    if (f1.isFile()) {
    	System.out.println(f1.getName() +"�@�̓t�@�C���ł��B");
    } else if (f1.isDirectory()){
    	System.out.println(f1.getName() +" �̓f�B���N�g���ł��B");
    }
    if (f1.canRead()){
    	System.out.println(f1.getName() + "�@�͓ǂݍ��߂܂��B");
    }
    if (f1.canWrite()){
    	System.out.println(f1.getName() + "�@�͏������߂܂��B");
    }
    System.out.println("�t�@�C�����F " + f1.length());

    /* �t�@�C���X�g���[���쐬 */
    if (f1.isFile()) {
        try {
            InputStream in = new FileInputStream(f1);
            OutputStream out = System.out;
            int ch;
            while ((ch = in.read()) != -1){
                ch = ch ^ 1;
                ch = ch ^ 1;
                out.write(ch);
            }
            in.close();
            out.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

    }
  }
}
